//
//  CZStatusoriginnaView.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/14.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit
import SDWebImage
class CZStatusoriginnaView: UIView {

    var status : CZStatusModel? {
    
        didSet{
            //拿到数据设置图片
            if let profile_image_url = status?.user?.profile_image_url {
                let URL = NSURL(string: profile_image_url)
                
                 self.iconView.sd_setImageWithURL(URL, placeholderImage: UIImage.init(named: "avatar"))
                
//                //将数据给pictureView
//                pictureView.pictureURLs = status?.storePicutreURLs
            }
            //名称
            self.nameLabel.text = status?.user?.screen_name
            self.timeLabel.text = status?.created_at
            self.sourceLabel.text = status?.source
            self.contentLabel.text = status?.text
            
            //设置V图片(好的数据应该都疯转好  不要vc或者Vm去在此设置数据)
            self.verifiedView.image = status?.user?.verifieldImage

            //设置会员信息
            self.memberView.image = status?.user?.mbrankImage
       
            
            //将微博图片URL传给pictureView 
            pictureView.pictureURLs = status?.storePicutreURLs
            
            pictureView.snp_updateConstraints { (make) in
                let margin = status?.storePicutreURLs == nil ? 0:HMStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
                
            }
        
        }
        
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        self.backgroundColor = UIColor.whiteColor()
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setUI(){
        // 1.添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 2.添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: HMStatusIconWH, height: HMStatusIconWH))
        }
        
        // 名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }
        
        /// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
//            make.bottom.equalTo(contentLabel)
            make.bottom.equalTo(pictureView)
        }

    
        
        
    
    }
    
// MARK: - ******************* #懒加载# ********************
    
    
    
    private lazy var pictureView: CZStatusPictureView = CZStatusPictureView()
    private lazy var bottomView : CZStatusBottomView = CZStatusBottomView()
    /// 头像
    private lazy var iconView: UIImageView = {
    
     let imageView = UIImageView(image: UIImage(named: "avatar"))

        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
    
    
        return imageView
    }()
    
    
    
    
    /// 用户名称Label
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFontOfSize(HMStatusNameFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    /// 时间label
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFontOfSize(HMStatusTimeFontSize)
        label.textColor = UIColor.orangeColor()
        
        return label
    }()
    
    /// 来源label
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFontOfSize(HMStatusTimeFontSize)
        label.textColor = UIColor.lightGrayColor()
        
        return label
    }()
    
    /// 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFontOfSize(HMStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        let result = arc4random_uniform(100) % 2
        label.text = result == 0 ? "要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:" : "明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨"
        label.sizeToFit()
        label.numberOfLines = 0
        
        return label
    }()

}
